package com.example.demo.annotation;

import org.springframework.integration.redis.util.RedisLockRegistry;

import java.util.concurrent.locks.Lock;

/**
 * @Auther Created by l.wang on 2019/1/15.
 */
public class RedisDistributedLock implements IDistributedLock {
    private RedisLockRegistry redisLockRegistry;

    public RedisDistributedLock(RedisLockRegistry redisLockRegistry) {
        this.redisLockRegistry = redisLockRegistry;
    }

    @Override
    public Lock lock(String lockName) {
        Lock lock = redisLockRegistry.obtain(lockName);
//        lock.tryLock();
        lock.lock();
        return lock;
    }

    @Override
    public void unlock(Lock lock) {
        lock.unlock();
    }

    public void setRedisLockRegistry(RedisLockRegistry redisLockRegistry) {
        this.redisLockRegistry = redisLockRegistry;
    }
}
